﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for tbPartsDB
/// </summary>
public class tbPartsDB
{
    #region[tbParts_Add]
    public static bool tbParts_Add(tbPartsDATA _tbPartsDATA)
    {
        bool check = false;
        using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_tbParts_Add", dbConn))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbConn.Open();
                dbCmd.Parameters.Add(new SqlParameter("@pId", _tbPartsDATA.pId));
                dbCmd.Parameters.Add(new SqlParameter("@Name", _tbPartsDATA.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", _tbPartsDATA.Ord));
                dbCmd.ExecuteNonQuery();
                dbConn.Close();
                check = true;
            }
        }
        return check;
    }
    #endregion
    #region[tbParts_Update]
    public static bool tbParts_Update(tbPartsDATA _tbPartsDATA)
    {
        bool check = false;
        using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_tbParts_Update", dbConn))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbConn.Open();
                dbCmd.Parameters.Add(new SqlParameter("@ID", _tbPartsDATA.ID)); 
                dbCmd.Parameters.Add(new SqlParameter("@pId", _tbPartsDATA.pId));
                dbCmd.Parameters.Add(new SqlParameter("@Name", _tbPartsDATA.Name));
                dbCmd.Parameters.Add(new SqlParameter("@Ord", _tbPartsDATA.Ord));
                dbCmd.ExecuteNonQuery();
                dbConn.Close();
                check = true;
            }
        }
        return check;
    }
    #endregion
    #region[tbParts_Delete]
    public static bool tbParts_Delete(string ID)
    {
        bool check = false;
        using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_tbParts_Delete", dbConn))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbCmd.Parameters.Add(new SqlParameter("@ID", ID));
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
                dbConn.Close();
                check = true;
            }
        }
        return check;
    }
    #endregion
    #region[tbParts_GetByID]
    public static List<tbPartsDATA> tbParts_GetByID(string ID)
    {
        List<tbPartsDATA> list = new List<tbPartsDATA>();
        using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_tbParts_GetByID", dbConn))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbConn.Open();
                dbCmd.Parameters.Add(new SqlParameter("@ID", ID));
                using (SqlDataReader reader = dbCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        tbPartsDATA objtbPartsDATA = new tbPartsDATA(
                        reader["ID"].ToString(),
                        reader["pId"].ToString(),
                        reader["Name"].ToString(),
                        reader["Ord"].ToString());
                        list.Add(objtbPartsDATA);
                    }
                }
                dbConn.Close();
            }
        }
        return list;
    }
    #endregion
    #region[tbParts_GetBypId]
    public static List<tbPartsDATA> tbParts_GetBypId(string pId)
    {
        List<tbPartsDATA> list = new List<tbPartsDATA>();
        using (SqlConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
        {
            using (SqlCommand dbCmd = new SqlCommand("sp_tbParts_GetBypId", dbConn))
            {
                dbCmd.CommandType = CommandType.StoredProcedure;
                dbConn.Open();
                dbCmd.Parameters.Add(new SqlParameter("@pId", pId));
                using (SqlDataReader reader = dbCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        tbPartsDATA objtbPartsDATA = new tbPartsDATA(
                        reader["ID"].ToString(),
                        reader["pId"].ToString(),
                        reader["Name"].ToString(),
                        reader["Ord"].ToString());
                        list.Add(objtbPartsDATA);
                    }
                }
                dbConn.Close();
            }
        }
        return list;
    }
    #endregion
}
